import axios from 'axios';
import {message as Message} from 'antd';
// import loading from './loading'

// 创建axios实例
export const service = axios.create({
    // baseURL: "http://112.74.61.241:8081",
    withCredentials: true,
    timeout: 10000, // 请求超时时间
})

/**
   * @description 拦截器，用于权限的管理
*/
// request拦截器
service.interceptors.request.use(config => {
    config.headers = {
      Authorization: `Bearer ${localStorage.getItem('UECP_token')}`,
      userId: `${localStorage.getItem('UECP_userId')}`,
      challenge: `${sessionStorage.getItem("challenge")}`,
    //   xip: sessionStorage.getItem("Ip"),
      'Content-Type': 'application/json;charset=UTF-8'
    }
    // loading.showLoding()
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
// resopnse拦截器
service.interceptors.response.use(response => {
    // setTimeout(() => {
    //     loading.hideLoding()
    // }, 300);
    // let { statusCode } = response.data
    // // token验证失败，重新登录
    // if(statusCode === 20020) {
    //   removetoken()
    // }

    // if(statusCode === 20021) {
    //   Message.error('网关失败')
    // }

    if (response) {
      return response
    }
},
    err => {
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    err.message = "错误请求"
                    break
                case 401:
                    err.message = "未授权，请重新登录！"
                    break
                case 403:
                    err.message = "拒绝访问"
                    break
                case 404:
                    err.message = "请求错误,未找到该资源！"
                    break
                case 405:
                    err.message = "请求方法未允许"
                    break
                case 408:
                    err.message = "请求超时"
                    break
                case 500:
                    err.message = "服务器端出错"
                    break
                case 501:
                    err.message = "网络未实现"
                    break
                case 502:
                    err.message = "网络错误"
                    break
                case 503:
                    err.message = "服务不可用"
                    break
                case 504:
                    err.message = "网络超时"
                    break
                case 505:
                    err.message = "http版本不支持该请求"
                    break
                default:
                    err.message = `连接错误${err.response.status}`
            }
        } else {
            err.message = "连接到服务器失败（超时）"
        }

        if (err && err.response) {
            Message.error(err.message)
        }
        // setTimeout(() => {
        //     loading.hideLoding()
        // }, 300);
        return Promise.reject(err)
    }
)
